package com.deyuanyun.pic.settings.service.org.impl;

import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.settings.controller.dto.org.PostDTO;
import com.deyuanyun.pic.settings.controller.dto.org.PostVO;
import com.deyuanyun.pic.settings.domain.org.OrgInfo;
import com.deyuanyun.pic.settings.domain.org.Post;
import com.deyuanyun.pic.settings.domain.util.DomainInitUtil;
import com.deyuanyun.pic.settings.mapper.org.CompanyMapper;
import com.deyuanyun.pic.settings.mapper.org.OrgInfoMapper;
import com.deyuanyun.pic.settings.mapper.org.PostMapper;
import com.deyuanyun.pic.settings.mapper.prvlg.UserMapper;
import com.deyuanyun.pic.settings.service.org.PostService;
import com.github.pagehelper.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PostServiceImpl implements PostService {

	@Resource
	private PostMapper postMapper;
	@Resource
	private CompanyMapper companyMapper;
	@Resource
	private UserMapper userMapper;
	@Resource
	OrgInfoMapper orgInfoMapper;

    /**
     * 查询岗位信息
     * @param postVO
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-12-29
     */
	@Override
	public List<PostDTO> queryAll(PostVO postVO) {
		return postMapper.queryAll(postVO);
	}

    /**
     * 保存岗位信息
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-12-29
     */
	@Override
	public void saveInfo(PostVO postVO) {
		Post info = new Post();
		BeanUtils.copyProperties(postVO, info);
		DomainInitUtil.initCreateDomain(info);
		info.setState((short) 0);
		String id = info.getId();
		String shortName = info.getShortName();

		Asserts.assertTrue(!StringUtils.isEmpty(shortName), "岗位名称不能为空");
		Asserts.assertTrue(!StringUtils.isEmpty(info.getCompanyId()), "上级公司不能为空");
		Asserts.assertTrue(!StringUtils.isEmpty(info.getDeptId()), "上级部门不能为空");

		PostVO vo = new PostVO();
		vo.setShortName(shortName);
		List list = postMapper.queryAll(vo);
		Asserts.assertTrue(list.isEmpty(), "系统已经存在相同的岗位名称");
		postMapper.insertSelective(info);

		OrgInfo record = new OrgInfo();
		BeanUtils.copyProperties(info, record);
		record.setType("2");
		record.setOrgId(id);
		record.setParentId(info.getDeptId());
		record.setId(id);
		record.setIdPath(id);
		String parentId = info.getParentId();

		if(StringUtils.isNotEmpty(parentId)){
			Map map = orgInfoMapper.findOrg(parentId);
			record.setIdPath(map.get("ID_PATH") + "#"+ record.getIdPath());
		}
		orgInfoMapper.insertSelective(record);
	}

    /**
     * 更新岗位信息
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-12-29
     */
	@Override
	public void updateInfo(PostVO postVO) {
        Post info = new Post();
        BeanUtils.copyProperties(postVO, info);
        DomainInitUtil.initUpdateDomain(info);
        String id = info.getId();
        String shortName = info.getShortName();

        Asserts.assertTrue(!StringUtils.isEmpty(shortName), "岗位名称不能为空");
        Asserts.assertTrue(!StringUtils.isEmpty(info.getCompanyId()), "上级公司不能为空");
        Asserts.assertTrue(!StringUtils.isEmpty(info.getDeptId()), "上级部门不能为空");
        postMapper.updateByPrimaryKeySelective(info);

        OrgInfo record = new OrgInfo();
        BeanUtils.copyProperties(info, record);

        record.setType("2");
        record.setOrgId(id);
        record.setParentId(info.getDeptId());
        record.setId(id);
        record.setIdPath(id);
        String parentId = info.getParentId();
        if(StringUtils.isNotEmpty(parentId)){
            Map map = orgInfoMapper.findOrg(parentId);
            record.setIdPath(map.get("ID_PATH") + "#" + record.getIdPath());
        }
        orgInfoMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 删除岗位信息
     * @param
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-12-29
     */
	@Override
	public int deleteInfo(List<String> idList) {
		HashMap param = new HashMap();
        param.put("DEPTList", idList);
        param.put("idList", idList);
		Page page = userMapper.findPage(param);
		Asserts.assertTrue(page.isEmpty(), "岗位不能被删除,有人已经挂在本岗位");
		int n = postMapper.deleteByPrimaryKey(param);
        for(int i = 0, len = idList.size(); i < len; i++) {
            String id = idList.get(i);
            if(StringUtils.isNotEmpty(id)) {
                orgInfoMapper.deleteByPrimaryKey(id);
            }
        }
        return n;
	}

    /**
     * 更新岗位启用状态
     * @param
     * @return 
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-12-30
     */
	@Override
	public int updateState(HashMap param) {
		return postMapper.updateState(param);
	}

}
